home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH7 / SRC / OBJ4POLY.CLS < prev    next >
Encoding:
Text File  |  1996-05-04  |  4.1 KB  |  158 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "ObjPolygon"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = False
  8. Option Explicit
  9.  
  10. Private num_points As Integer
  11. Private y() As Single
  12. Private x() As Single
  13.  
  14. Public Drawn As Boolean
  15. ' ************************************************
  16. ' Return True if this polygon contains the
  17. ' indicated point.
  18. ' ************************************************
  19. Function Contains(x As Single, y As Single) As Boolean
  20. Dim xmin As Single
  21. Dim ymin As Single
  22. Dim xmax As Single
  23. Dim ymax As Single
  24.  
  25.     Bound xmin, ymin, xmax, ymax
  26.     Contains = (x >= xmin And x <= xmax And _
  27.                 y >= ymin And y <= ymax)
  28. End Function
  29.  
  30. Function ObjectType() As String
  31.     ObjectType = "POLYGON"
  32. End Function
  33.  
  34. ' ************************************************
  35. ' Set the coordinates for a point.
  36. ' ************************************************
  37. Sub SetPoint(Index As Integer, x1 As Single, y1 As Single)
  38.     If Index < 1 Or Index > num_points Then Exit Sub
  39.     x(Index) = x1
  40.     y(Index) = y1
  41. End Sub
  42.  
  43. ' ************************************************
  44. ' Apply a transformation matrix to the object.
  45. ' ************************************************
  46. Sub Transform(M() As Single)
  47. Dim i As Integer
  48.  
  49.     For i = 1 To num_points
  50.         m2PointMultiply x(i), y(i), M
  51.     Next i
  52. End Sub
  53.  
  54. ' ************************************************
  55. ' Apply a nonlinear transformation.
  56. ' ************************************************
  57. Sub Distort(d As Object)
  58. Dim i As Integer
  59.  
  60.     For i = 1 To num_points
  61.         d.Distort x(i), y(i)
  62.     Next i
  63. End Sub
  64. ' ************************************************
  65. ' Set the number of points and redimension the
  66. ' point arrays.
  67. ' ************************************************
  68. Property Let NumPoints(n As Integer)
  69.     If n < 1 Then
  70.         Erase x
  71.         Erase y
  72.         num_points = 0
  73.         Exit Property
  74.     End If
  75.     
  76.     num_points = n
  77.     ReDim x(1 To num_points)
  78.     ReDim y(1 To num_points)
  79. End Property
  80. ' ************************************************
  81. ' Compute the world coordinate bounds for the
  82. ' polygon.
  83. ' ************************************************
  84. Sub Bound(xmin As Single, ymin As Single, xmax As Single, ymax As Single)
  85. Dim i As Integer
  86.  
  87.     If num_points = 0 Then
  88.         xmin = 0
  89.         xmax = 0
  90.         ymin = 0
  91.         ymax = 0
  92.         Exit Sub
  93.     End If
  94.     
  95.     xmin = x(1)
  96.     xmax = x(1)
  97.     ymin = y(1)
  98.     ymax = y(1)
  99.     For i = 2 To num_points
  100.         If x(i) < xmin Then xmin = x(i)
  101.         If y(i) < ymin Then ymin = y(i)
  102.         If x(i) > xmax Then xmax = x(i)
  103.         If y(i) > ymax Then ymax = y(i)
  104.     Next i
  105. End Sub
  106.  
  107. ' ************************************************
  108. ' Write a polygon to a file using Write.
  109. ' Begin with "POLYGON" to identify this object.
  110. ' ************************************************
  111. Sub FileWrite(filenum As Integer)
  112. Dim i As Integer
  113.  
  114.     Write #filenum, "POLYGON", num_points
  115.     For i = 1 To num_points
  116.         Write #filenum, x(i), y(i)
  117.     Next i
  118. End Sub
  119. ' ************************************************
  120. ' Draw the polygon on a Form, Printer, or
  121. ' PictureBox.
  122. ' ************************************************
  123. Sub Draw(canvas As Object)
  124. Dim i As Integer
  125.  
  126.     ' Don't draw if there are no points.
  127.     If num_points < 1 Then Exit Sub
  128.     
  129.     ' Don't draw if it has been drawn before.
  130.     If Drawn Then Exit Sub
  131.     Drawn = True
  132.     NumDraws = NumDraws + 1
  133.     
  134.     canvas.CurrentX = x(1)
  135.     canvas.CurrentY = y(1)
  136.     For i = 2 To num_points
  137.         canvas.Line -(x(i), y(i))
  138.     Next i
  139. End Sub
  140.  
  141. ' ************************************************
  142. ' Read a polygon from a file using Input.
  143. ' Assume the "POLYGON" label has already been read.
  144. ' ************************************************
  145. Sub FileInput(filenum As Integer)
  146. Dim i As Integer
  147.  
  148.     Input #filenum, num_points
  149.     If num_points < 1 Then Exit Sub
  150.     ReDim x(1 To num_points)
  151.     ReDim y(1 To num_points)
  152.     For i = 1 To num_points
  153.         Input #filenum, x(i), y(i)
  154.     Next i
  155. End Sub
  156.  
  157.  
  158.